$(function() {

    initValidation();

    showData();
    
    $('#btnSend').click(function(){
        $(this).closest('form').submit();
        return false;
    });
    
    $('#contacts-form').submit(function(){
        if($(this).valid()) {
            alert('Message Sent');
            $(this).find('#txtMessage').val('');
        }
        return false;    
    });
});

function initValidation() {
    $('#contacts-form').validate({
        rules: {
            'txtName': {
                required: true,
                rangelength: [6, 50],
            },
            'txtEmail': {
                required: true,
                email: true,
            },
            'txtMessage': {
                required: true,
            },
        },
        messages: {
            'accUsername': {
                required: 'Name is required.',
                rangelength: 'Name length must be between 6 and 50.',
            },
            'txtEmail': {
                required: 'Email is required.',
                email: 'Email invalid.',
            },
            'txtMessage': {
                required: 'Message is required.',
            },
        },
        errorContainer: $('.errorContainer'),
        errorLabelContainer: $('.errorContainer ul'),
        wrapper: 'li'
    });
}

function showData() {
    if($.cookies.get('user') !== null) {
        var user = $.cookies.get('user');
        $('#txtName').val(user.firstName + ' ' + user.lastName);
        $('#txtEmail').val(user.email);
    }
}